﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.OleDb;

namespace DAO
{
    public class LoaiKhachDAO: AbtractDAO
    {
        public static List<LoaiKhachDTO> LayDSLoaiKhach()
        {
            OleDbConnection ketNoi = null;
            List<LoaiKhachDTO> dsLoaiKhach = new List<LoaiKhachDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaLoaiKhach,TenLoaiKhach,HeSo FROM LOAIKHACHHANG";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbDataReader boLoc = lenh.ExecuteReader();

                while (boLoc.Read())
                {
                    LoaiKhachDTO LoaiKhach = new LoaiKhachDTO();
                    LoaiKhach.MaLoaiKhach = boLoc.GetInt32(0);

                    if (!boLoc.IsDBNull(1))
                    {
                        LoaiKhach.TenLoaiKhach = boLoc.GetString(1);
                    }

                    if (!boLoc.IsDBNull(2))
                    {
                        LoaiKhach.HeSo = boLoc.GetDouble(2);
                    }

                    dsLoaiKhach.Add(LoaiKhach);
                }
            }
            catch (Exception ex)
            {
                dsLoaiKhach = new List<LoaiKhachDTO>();
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return dsLoaiKhach;
        }

        public static bool ThemLoaiKhach(string tenLoaiKhach, double heSo)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "INSERT INTO LOAIKHACHHANG(TenLoaiKhach,HeSo) VALUES(@TenLoaiKhach,@HeSo)";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@TenLoaiKhach", OleDbType.VarChar);
                thamSo.Value = tenLoaiKhach;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@HeSo", OleDbType.Double);
                thamSo.Value = heSo;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketQua = false;
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }

        public static void XoaLoaiKhach(long maLoaiKhach)
        {
            OleDbConnection ketNoi = null;

            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "DELETE FROM LOAIKHACHHANG WHERE MaLoaiKhach=@MaLoaiKhach";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@MaLoaiKhach", OleDbType.Integer);
                thamSo.Value = maLoaiKhach;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }

        public static void UpdateLoaiKhach(long maLoaiKhach, string tenLoaiKhach, double heSo)
        {
            OleDbConnection ketNoi = null;

            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "UPDATE LOAIKHACHHANG SET TenLoaiKhach=@TenLoaiKhach,HeSo=@HeSo WHERE MaLoaiKhach=@MaLoaiKhach";

                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;


                thamSo = new OleDbParameter("@TenLoaiKhach", OleDbType.VarChar);
                thamSo.Value = tenLoaiKhach;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@HeSo", OleDbType.Double);
                thamSo.Value = heSo;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaLoaiKhach", OleDbType.Integer);
                thamSo.Value = maLoaiKhach;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
        }
    }
}
